import { createAction } from 'redux-actions'
import { delJson, body, putJson, postJson, check, parseJSON, checkStatus, data, getJson } from './util'
import util from '../util'
import fetch from 'isomorphic-fetch'

export 
const fetchProfile = createAction('fetch profile', (userId, cb) =>
  fetch(`/users/${userId}/core-profile`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb();
      return res.data;
    })
)

export
const moveDept = createAction("move department", (data, cb)=>{
  // data => {orgId, content, userId, teamSorts}
  return(
    fetch(`/okrs/objective/move`, {
      ...postJson,
      ...body(data),
    })
      .then(checkStatus)
      .then(parseJSON)
      .then(check)
      .then(cb)
  )
});


/**临时添加一个获取用户profile的接口
 * 读取onboardingTemplate的信息，
 * 用于区分CSV导入的用户
 * 屏蔽我的文档的入口
 * 后端在0.9.2版本会把onboardingTemplate的信息
 * 封装到coreProfile接口
* */
export
const fetchUserProfile = createAction('fetch my profile', (userId, cb) =>
	fetch(`/users/${userId}/complete-profile`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(data)
)

export
const fetchUserIdentifyProfile = createAction("fetch user Identify profile", (encryptedUserId, encryptedOrgId, cb) => {
  let arg = {
    encryptedUserId,
    encryptedOrgId
  };
  return fetch(`/users/user-profile?${util.body(arg)}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(data)
})

/***/

export 
const fetchUnreadNumber = createAction('fetch unread number', userId =>
  fetch(`/messages/get-unread-number`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
)

// 读取消息弹层列表
export
const fetchLiveNotificationList = createAction('fetch live notification list', (cb) =>
  fetch('/messages/live', getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb(res.data)
      return res.data
    })
)

export 
const fetchAddresses = createAction('fetch addresses', parentId =>
	fetch(`/addresses?parentId=${parentId}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(res => ({
			parentId,
			...res.data
		}))
)

export
const updateAvatar = createAction('update avatar', (arg, cb) => {
    arg = Object.keys(arg).map(key => `${key}=${arg[key]}`).join('&')
    return (
      fetch(`/avatars?${arg}`, getJson)
        .then(checkStatus)
        .then(parseJSON)
        .then(check)
        .then(res => {
          cb && cb(res.data)
          return res.data
        })
    )
  }
)

export
const updateStaffAvatar = createAction('_', (arg, cb) => {
    arg = Object.keys(arg).map(key => `${key}=${arg[key]}`).join('&')
    return (
      fetch(`/onboarding-flows/staff/avatars?${arg}`, getJson)
        .then(checkStatus)
        .then(parseJSON)
        .then(check)
        .then(res => {
          cb && cb(res.data)
          return res.data
        })
    )
  }
)

export
const showGuide = createAction('show guide');

export
const clearGuide = createAction('clear guide');

export
const nextGuide = createAction('next guide');

export
const clearNextGuide = createAction('clear next guide');

export
const setStepGuide = createAction('set step guide', step => step);

export
const updateGuide = createAction('update guide', step =>
  fetch('/navigation', {
    ...putJson,
    ...body({
      naviStep: step,
    })
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(() => step)
)

export
const getGuide = createAction('get guide', () =>
  fetch('/navigation', getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(data)
)

export
const deleteGuide = createAction('_', cb =>
  fetch('/navigation',  delJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(cb)
)

export const freezeGuide = createAction('freeze guide', freeze => freeze);
